/*
 * @Copyright: Citylife 2021
 * @Author: WangXinnong
 * @Date: 2020-12-14 12:48:48
 * @Description: DialogUtilWidget
 */
import 'package:flutter/material.dart';

import '../../components/button.dart';
import '../../components/gesture_avoid_detector.dart';
import '../sizer/sizer_ext.dart';
import '../theme/theme_manager.dart';
import 'dialog_manager.dart';

typedef ActionFunction = void Function();

class DialogUtilWidget {
  // confirm弹框，有内容和两个按钮弹框
  Widget confirmDialogWidget(BuildContext context, String message,
      {Key key,
      String leftActionTitle = "取消",
      String rightActionTitle = "确定",
      ActionFunction leftAction,
      ActionFunction confirmAction}) {
    return Container(
      key: key,
      margin: EdgeInsets.symmetric(horizontal: 58.0.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8.0.w),
        color: theme.colors.dialogCardBackgroundColor,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.all(20.0.w),
            child: Text(
              message ?? "",
              key: ValueKey('test_down_dish_title_$message'),
              style: TextStyle(
                color: theme.colors.dialogTitleColor,
                fontSize: 16.0.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          Divider(
            height: 0,
            thickness: 1.0,
            color: theme.colors.defaultSeparatorColor,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: RadiusButton(
                  title: leftActionTitle ?? "取消",
                  fontSize: 16.0.sp,
                  height: 48.0.w,
                  titleColor: theme.colors.dialogCancelColor,
                  color: Colors.transparent,
                  onPressed: () {
                    DialogManager.instance.dismiss();
                    if (leftAction != null) {
                      leftAction();
                    }
                  },
                ),
              ),
              Container(
                height: 48.0.w,
                width: 1.0,
                color: theme.colors.defaultSeparatorColor,
              ),
              Expanded(
                child: RadiusButton(
                  title: rightActionTitle ?? "确定",
                  fontSize: 16.0.sp,
                  height: 48.0.w,
                  titleColor: theme.colors.dialogConfirmColor,
                  color: Colors.transparent,
                  onPressed: () {
                    DialogManager.instance.dismiss();
                    if (confirmAction != null) {
                      confirmAction();
                    }
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

// tips弹框，只有一个按钮和内容的弹框
  Widget tipsDialogWidget(BuildContext context, String message,
      {Key key, String actionTitle = "确定", ActionFunction action}) {
    return Container(
      key: key,
      margin: EdgeInsets.symmetric(horizontal: 58.0.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8.0.w),
        color: theme.colors.dialogCardBackgroundColor,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.all(20.0.w),
            child: Text(
              message ?? "",
              style: TextStyle(
                color: theme.colors.dialogTitleColor,
                fontSize: 16.0.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          Divider(
            height: 0,
            thickness: 1.0,
            color: theme.colors.defaultSeparatorColor,
          ),
          RadiusButton(
            title: actionTitle ?? "确定",
            fontSize: 16.0.sp,
            height: 48.0.w,
            titleColor: theme.colors.dialogConfirmColor,
            color: Colors.transparent,
            onPressed: () {
              DialogManager.instance.dismiss();
              if (action != null) {
                action();
              }
            },
          ),
        ],
      ),
    );
  }

// 结果弹框，包含两种样式，成功、失败，默认成功，如果传入leftActionTitle不为空，则展示两个按钮，如果为空，则展示一个按钮actionTitle
  Widget resultDialogWidget(BuildContext context, String message,
      {String imageUrl = 'assets/images/ic_bindgou.png',
      String leftActionTitle,
      String actionTitle = '确认',
      ActionFunction leftAction,
      ActionFunction confirmAction}) {
    List<String> titles = [];
    if (leftActionTitle != null && leftActionTitle.length > 0) {
      titles.add(leftActionTitle);
    }
    titles.add(actionTitle);
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 46),
      padding: EdgeInsets.fromLTRB(16, 28, 16, 16),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: theme.colors.dialogCardBackgroundColor,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Image.asset(
            imageUrl,
            width: 110,
            height: 110,
            fit: BoxFit.fill,
          ),
          SizedBox(
            height: 20,
          ),
          Text(
            message ?? "",
            style: TextStyle(
              height: 1,
              color: theme.colors.dialogTitleColor,
              fontSize: 18,
              fontWeight: FontWeight.w500,
            ),
          ),
          SizedBox(
            height: 32,
          ),
          _resultBottomWidget(titles, leftAction, confirmAction),
        ],
      ),
    );
  }

  Widget imgDialogWidget(
    String message, {
    ActionFunction confirmAction,
    String actionTitle = '重新登录',
    double imageWidth,
    String imagePath,
  }) {
    return Container(
      width: 244.0.w,
      padding: EdgeInsets.only(bottom: 24.0.w),
      child: Stack(
        alignment: Alignment.topCenter,
        children: [
          Container(
            padding: EdgeInsets.only(top: 117.0.w),
            height: 309.0.w,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(20.0.w)),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(
                    height: 75.0.w,
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 15.0.w),
                    child: Text(
                      message,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontSize: 16.0.sp, fontWeight: FontWeight.bold),
                    ),
                  ),
                  Spacer(),
                  GestureAvoidDetector(
                    onTap: () {
                      DialogManager.instance.dismiss();
                      confirmAction();
                    },
                    child: Container(
                      height: 48.0.w,
                      decoration: BoxDecoration(
                        border: Border(
                            top: BorderSide(
                          color: theme.colors.defaultSeparatorColor,
                          width: 0.5,
                        )),
                      ),
                      child: Center(
                        child: Text(
                          actionTitle,
                          style: TextStyle(
                              fontSize: 16.0.sp,
                              color: theme.colors.dialogConfirmColor,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Positioned(
            child: Image(
              image: AssetImage(imagePath),
              width: imageWidth,
              fit: BoxFit.fitWidth,
            ),
          )
        ],
      ),
    );
  }

  _resultBottomWidget(List<String> titles, ActionFunction leftAction,
      ActionFunction confirmAction) {
    if (titles.length == 2) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: RadiusButton(
              title: titles.first,
              fontSize: 14,
              height: 38,
              titleColor: theme.colors.dialogCancelColor,
              onPressed: () {
                DialogManager.instance.dismiss();
                if (leftAction != null) {
                  leftAction();
                }
              },
            ),
          ),
          SizedBox(
            width: 10,
          ),
          Expanded(
            child: RadiusButton(
              title: titles.last,
              fontSize: 14,
              height: 38,
              titleColor: theme.colors.dialogConfirmColor,
              onPressed: () {
                DialogManager.instance.dismiss();
                if (confirmAction != null) {
                  confirmAction();
                }
              },
            ),
          ),
        ],
      );
    }
    return RadiusButton(
      title: titles.first,
      fontSize: 14,
      height: 38,
      titleColor: theme.colors.dialogConfirmColor,
      onPressed: () {
        DialogManager.instance.dismiss();
        if (confirmAction != null) {
          confirmAction();
        }
      },
    );
  }
}
